package com.lcc.lynx.base.system.dict;

import com.lcc.lynx.base.system.enums.DictEnum;
import com.lcc.lynx.base.system.pojo.dv.SysDictDV;
import com.lcc.lynx.base.system.pojo.dv.SysDictDtlDV;
import com.lcc.lynx.base.system.pojo.dv.SysRoleDV;
import com.lcc.lynx.base.system.service.SysRoleService;
import com.lcc.lynx.base.system.util.CacheUtil;
import com.lcc.lynx.common.dict.IDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysUserRoleDict implements IDictService<SysDictDV> {

    @Autowired
    private SysRoleService roleService;

    @Override
    public List<SysDictDtlDV> execute(SysDictDV dto) {
        SysRoleDV sysRoleDV = new SysRoleDV();
        List<SysDictDtlDV> list = CacheUtil.getDict(dto.getDictCode());
        if(list == null){
            list = roleService.select(sysRoleDV).stream().map(item -> {
                SysDictDtlDV dictDtlDV = new SysDictDtlDV();
                dictDtlDV.setDictLabel(item.getRoleName());
                dictDtlDV.setDictValue(item.getRoleId().toString());
                return dictDtlDV;
            }).collect(Collectors.toList());
            CacheUtil.setDict(dto.getDictCode(), list);
        }
        return list;
    }

    @Override
    public String getDictCode() {
        return DictEnum.SYS_USER_ROLE.name();
    }
}
